2020-2021 Sunseeker Telemetry and Lighting System
timer_d_ex5_hiResFreeRunningPWM.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430F51x2 Demo - TimerD0, Hi-Res Free Running mode, PWM TD0.0-2, TDCLK = 64Mhz
34 //
35 // Description: This code example shows how to configure TimerD in Hi-Res
36 // Free Running mode. The TD0HCTL1 register with the TDHCLKTRIMx, TDHCLKSRx and
37 // TDHCLKRx bit settings are configured with the calibration data in the TLV
38 // table.
39 //
40 // SMCLK = MCLK = DCOclock = default DCO; TDCLK(TLV Cal Data)= 64Mhz
41 // Note: 1. TDHMx=00(8x) should be used to use the 64Mhz TimerD TLV calibration data
42 // 2. Use code example with hal_tlv.c and hal_tlv.h files to read the TimerD TLV data
43 //
44 // MSP430F51x2
45 // -----------------
46 // /|\| XIN|-
47 // | | |
48 // --|RST XOUT|-
49 // | |
50 // | P1.6/TD0.0|--> CCR0 - 50% dutycycle; PWM period = (2 x 128)/64MHz~ 4us = 250kHz
51 // | P1.7/TD0.1|--> CCR1 - 25% dutycycle; ON period ~ 32/64MHz~ 500ns
52 // | P2.0/TD0.2|--> CCR2 - 75% dutycycle; ON period ~ 96/64MHz~ 1500ns
53 //
54 //******************************************************************************
55 #include "driverlib.h"
56 
57 void main(void)
58 {
59  struct s_TLV_Timer_D_Cal_Data * pTD0CAL; // Structure initialized in tlv.h
60  uint8_t bTD0CAL_bytes;
61 
62  // Stop WDT
63  WDT_A_hold(WDT_A_BASE);
64 
65  //Get TimerD0 Cal Values (instance 0)
66  TLV_getInfo(TLV_TAG_TIMER_D_CAL, 0,
67  &bTD0CAL_bytes,
68  (uint16_t **) &pTD0CAL
69  );
70 
71  if(bTD0CAL_bytes == 0x0)
72  {
73  // No TimerD free running cal data found
74  while(1); // Loop here
75  }
76 
77  // Configure TimerD in Hi-Res Free Running Mode
78  Timer_D_initHighResGeneratorInFreeRunningMode(TIMER_D0_BASE,
79  TIMER_D_HIGHRES_64MHZ
80  );
81 
82  // Configure TD0.x GPIO pins
83  GPIO_setAsPeripheralModuleFunctionOutputPin(
84  GPIO_PORT_P1,
85  GPIO_PIN6 + GPIO_PIN7
86  );
87 
88  GPIO_setAsPeripheralModuleFunctionOutputPin(
89  GPIO_PORT_P2,
90  GPIO_PIN0
91  );
92 
93  // Configure the CCRx blocks
94  Timer_D_initCompareModeParam initComp1Param = {0};
95  initComp1Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_1;
96  initComp1Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
97  initComp1Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE_SET;
98  initComp1Param.compareValue = 32;
99  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp1Param);
100 
101  Timer_D_initCompareModeParam initComp2Param = {0};
102  initComp2Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_2;
103  initComp2Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
104  initComp2Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE_SET;
105  initComp2Param.compareValue = 96;
106  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp2Param);
107 
108  Timer_D_initCompareModeParam initComp3Param = {0};
109  initComp3Param.compareRegister = TIMER_D_CAPTURECOMPARE_REGISTER_0;
110  initComp3Param.compareInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
111  initComp3Param.compareOutputMode = TIMER_D_OUTPUTMODE_TOGGLE;
112  initComp3Param.compareValue = 128-1;
113  Timer_D_initCompareMode(TIMER_D0_BASE, &initComp3Param);
114 
115  Timer_D_initUpDownModeParam initUpDownParam = {0};
116  initUpDownParam.clockSource = TIMER_D_CLOCKSOURCE_EXTERNAL_TDCLK;
117  initUpDownParam.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
118  initUpDownParam.clockingMode = TIMER_D_CLOCKINGMODE_HIRES_LOCAL_CLOCK;
119  initUpDownParam.timerPeriod = 128-1;
120  initUpDownParam.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
121  initUpDownParam.captureCompareInterruptEnable_CCR0_CCIE =
122  TIMER_D_CAPTURECOMPARE_INTERRUPT_DISABLE;
123  initUpDownParam.timerClear = TIMER_D_DO_CLEAR;
124  Timer_D_initUpDownMode(TIMER_D0_BASE, &initUpDownParam);
125 
126  Timer_D_startCounter(TIMER_D0_BASE,
127  TIMER_D_UP_MODE
128  );
129 
130  __bis_SR_register(LPM0_bits + GIE); // Enter LPM0
131  __no_operation(); // For debugger
132 }
133 
__no_operation()
void main(void)